package com.pub.swd;

import com.pub.tools.PublicFunction;
import com.pub.web.WebFunction;
import java.util.ArrayList;
import com.pub.database.QueryBean;

public class SwdFunction {
    private QueryBean gqbnDAO=null;
    public SwdFunction(QueryBean pqbnDAO) {
        gqbnDAO=pqbnDAO;
    }
    /*--------------------------------------------------------------
     目的   :讀取Menu資料 (從Public_function.inc wf_read_menu翻寫過來)
     輸入參數:ps_frame_no,ps_user_no,ps_prg_lang,ps_srcpath
     傳回值  :讀取出來的資料
     -------------------------------------------------------------- */
    public String wf_read_menu(String ps_frame_no, String ps_user_no,
                               String ps_prg_lang, String ps_prg_srcpath) {
      //final String strMethodName = "wf_read_menu"; //method name
      String ls_rtn = ""; //用來回傳值用的
      String ls_sql = "", ls_frame_no,  ls_sys_group,
          ls_add;
      String[] ls_rtn_set;
      String[] ls_rtn_set2;
      String[][] ls_dat_set; //二維陣列
      int li_max;

      //if (blnDebug) System.out.println("\n" + strProgName + " (" + strMethodName + ") ===== Start =====");
      try {
        ls_frame_no = ps_frame_no;
        ls_sql = "SELECT sys_group FROM wkf_frame WHERE frame_no='" + ls_frame_no +
            "'";
        ls_rtn = wf_read_data(ls_sql);
        ls_rtn_set = PublicFunction.split(ls_rtn, "，");
        ls_sys_group = "(";
        for (int li_cnt = 0; li_cnt < ls_rtn_set.length; li_cnt++) {
          if (li_cnt > 0) {
            ls_sys_group = ls_sys_group + "," + "'" + ls_rtn_set[li_cnt] + "'";
          }
          else {
            ls_sys_group = ls_sys_group + "'" + ls_rtn_set[li_cnt] + "'";
          }
        } //End-for
        ls_sys_group = ls_sys_group + ")";

        ls_sql = "SELECT type_up || `^#` || type_id || `^#` || type_status || `^#` || type_id || " +
            " `＝ insFld(` || " + " type_up || `, gFld(~" +
            "` || type_name || `~,~` || url || `~))` " + " FROM wkf_menu " +
            " WHERE sys_no IN " + ls_sys_group + " AND type_status<>'N' AND ( type_status='C' OR type_status='O' OR ((type_status='P' OR type_status='D') AND type_up IN (SELECT type_id FROM wkf_menu WHERE sys_no IN " +
            ls_sys_group + " AND (type_status='C' OR type_status='O')) ) ) AND ( type_status='C' OR type_status='O' OR  type_id IN (SELECT prg_no FROM wkf_prg_right WHERE ( group_no IN (SELECT group_no FROM wkf_group WHERE user_no='" +
            ps_user_no + "') AND p1='Y'))) ORDER BY flag,seqno";
        ls_rtn = wf_read_data(ls_sql);
        ls_rtn = ls_rtn.replaceAll("~", "\"");
        ls_rtn = ls_rtn.replaceAll("～", "\"");
        ls_rtn = PublicFunction.replace(ls_rtn, "，", ",",false); //搭配read data將讀出的資料中的,轉為，
        ls_rtn = PublicFunction.replace(ls_rtn, "{prg_lang}", ps_prg_lang,false);
        ls_rtn = PublicFunction.replace(ls_rtn, "{srcpath}", ps_prg_srcpath,false); //hws0423
        ls_rtn = PublicFunction.replace(ls_rtn, "{user_id}", ps_user_no,false); //hws0512
        ls_rtn_set = PublicFunction.split(ls_rtn, "`");
        li_max = ls_rtn_set.length;
        ls_dat_set = new String[li_max][];
        ls_rtn = "";
        for (int li_cnt = 0; li_cnt < ls_rtn_set.length; li_cnt++) {
          ls_rtn_set2 = PublicFunction.split(ls_rtn_set[li_cnt].toString(), "^#");
          ls_dat_set[li_cnt] = new String[ls_rtn_set2.length]; //將陣列的第二個維度 New 出來
          for (int j = 0; j < ls_rtn_set2.length; j++) { //將陣列中的值搬給ls_dat_set
            ls_dat_set[li_cnt][j] = ls_rtn_set2[j].trim().toString();
          }
        } //End-for
        for (int li_cnt = 0; li_cnt < ls_rtn_set.length; li_cnt++) {
          ls_add = "N";
          if (ls_dat_set[li_cnt][2].trim().equalsIgnoreCase("C")) {
            for (int li_cnt1 = (li_cnt + 1); li_cnt1 < ls_rtn_set.length; li_cnt1++) {
              if (ls_dat_set[li_cnt1][0].trim().equalsIgnoreCase(ls_dat_set[
                  li_cnt][1])) {
                ls_add = "Y";
              } //End-if
            } //End-for
          } //End-if
          else {
            ls_add = "Y";
          }
          if (ls_add.trim().equalsIgnoreCase("Y")) {
            ls_rtn = ls_rtn + ls_dat_set[li_cnt][3].trim().toString() + "\n";
          }
        } //End-for
        //ls_rtn = ls_rtn.replaceAll(".asp",".jsp"); //將附檔名為asp的，轉成jsp  //hws0512 mark
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
        //if (blnDebug) System.out.println(strProgName + " (" + strMethodName + ") ===== End =====");
        
      }
      return ls_rtn;
  } //End - wf_read_menu

  /*--------------------------------------------------------------
    目的      : 讀取SQL資料
    輸入參數   : ps_sql
    傳回值     : 讀取出來的資料
   -------------------------------------------------------------- */
  public String wf_read_data(String ps_sql) {
    //final String strMethodName = "wf_read_data"; //method name
    String ls_all_rtn = ""; //用來回傳值用的
    String ls_sql, strTemp, ls_return, ls_value;
    String[] ls_sql_set;
    //if (blnDebug) System.out.println("\n" + strProgName + " (" + strMethodName + ") ===== Start =====");
    try {
      ls_sql = (ps_sql == null ? "" : ps_sql.trim());
      if (!ls_sql.equals("")) {
        ls_sql = WebFunction.replaceReserveCharOnPageSubmit(ls_sql); //取代特殊字元
        ls_sql_set = PublicFunction.split(ls_sql, "!^");
        ArrayList<ArrayList<Object>> arlData = null;
        ArrayList<Object> arlTemp = new ArrayList<Object>();
        for (int k = 0; k < ls_sql_set.length; k++) { //執行每一個sql
          strTemp = ls_sql_set[k];
          ls_return = "";
          if (!strTemp.trim().equals("")) {
            arlData = gqbnDAO.querySQL(strTemp);
            if (arlData != null) { //此行要記得判斷
              for (int i = 0; i < arlData.size(); i++) {
                if (!ls_return.equals("")) {
                  ls_return = ls_return + "`";
                }
                arlTemp = (ArrayList<Object>) arlData.get(i);
                for (int j = 0; j < arlTemp.size(); j++) {
                  ls_value = arlTemp.get(j).toString();
                  ls_value = PublicFunction.replace(ls_value, "'", "^*",false);
                  ls_value = PublicFunction.replace(ls_value, ",", "，",false);
                  ls_value = ls_value.replaceAll("\n", "chr(13)"); //替換掉跳行字元
                  ls_value = ls_value.replaceAll("\r", ""); //替換掉跳行字元
                  if (j == 0) {
                    ls_return = ls_return + ls_value;
                  }
                  else {
                    ls_return = ls_return + "," + ls_value;
                  }
                } //end-for
              } //end-for
            } //end-if (if (arlData != null) )
          } //End - if (! strTemp.trim().equals(""))
          else {
            ls_return = "";
          } //End - if (! strTemp.trim().equals(""))
          if (k != 0) {
            ls_all_rtn = ls_all_rtn + "!^" + ls_return;
          }
          else {
            ls_all_rtn = ls_return;
          }
        } //End-for
        arlData = null;
        arlTemp = null; //release resource
      } //end-if ! ls_sql.equals("")
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
      //if (blnDebug) System.out.println(strProgName + " (" + strMethodName + ") ===== End =====");
      
    }
    return ls_all_rtn;
  } //End - wf_read_data
}
